package com.jin.listener;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @Author: yuanxiuyao
 * @Date: 2024/1/5 9:06
 * @PackageName: org.example.listener
 * @Version 1.0
 */
@Component
public class SpringApplicationListener implements ApplicationListener<ApplicationStartedEvent> {

    @Resource
    private ApplicationContext applicationContext;

    private static List<Consumer<ApplicationContext>> listeners = new ArrayList<>();

    public static void addListener(Consumer<ApplicationContext> listener){
        listeners.add(listener);
    }

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        for (Consumer<ApplicationContext> listener : listeners) {
            listener.accept(applicationContext);
        }
    }
}
